/**
 * @type {Date}
 *
 * @properties={typeid:35,uuid:"EC918DF6-8CE6-43A5-BC01-88A051637457",variableType:93}
 */
var fFechaHasta = null;

/**
 * @type {Date}
 *
 * @properties={typeid:35,uuid:"FF2C0EB9-DE8D-4CDB-9FE5-F65B84786D48",variableType:93}
 */
var fFechaDesde = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"0AA3546B-A3C5-4CC8-BA5C-0F107B512CEF",variableType:4}
 */
var fCtos = null;

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"035CDF0A-00F5-40D1-A2BE-33BAE10D7FB4"}
 * @AllowToRunInFind
 */
function onAction(event) {
	/** @type {JSFoundSet<db:/infocom/ctacte_pr>}*/ 
	var fs_cc = databaseManager.getFoundSet('infocom','ctacte_pr')
	
	var $fecha = '#'+ utils.dateFormat(fFechaDesde,'dd-MM-yyyy')+'...'+utils.dateFormat(fFechaHasta,'dd-MM-yyyy')+'|dd-MM-yyyy'
	fs_cc.find()
	
	fs_cc.ctacte_pr_to_tab_mov_pr.diario_compras = 1
	fs_cc.fecha_mov = $fecha
		
	if (fs_cc.search() == 0)
	{
		globals.DIALOGS.showWarningDialog('Aviso','No se encontraron movimientos','OK')
		return
	}
	
	//Encontro movimientos
	/** @type {JSFoundSet<db:/infocom/compras_contable>} */
	var fs_compras = databaseManager.getFoundSet('infocom','compras_contable')
	fs_compras.loadAllRecords()
	fs_compras.deleteAllRecords()
	
	var cant = databaseManager.getFoundSetCount(fs_cc)
	fCtos = cant
	for (var i = 1 ; i <= cant ; i++)
	{
		var rcdCC = fs_cc.getRecord(i);
		fs_cc.setSelectedIndex(i)
		fs_compras.newRecord()
		var rcdCom = fs_compras.getRecord(1);
		//databaseManager.copyMatchingColumns(rcdCC,rcdCom,true)
		rcdCom.fecha_mov 			= rcdCC.fecha_mov
		rcdCom.letra 				= rcdCC.letra
		rcdCom.cod_mov 				= rcdCC.cod_mov
		rcdCom.nro_factura 			= rcdCC.nro_factura
		rcdCom.proveedor_id 		= rcdCC.proveedor_id
		rcdCom.importe 				= rcdCC.neto + rcdCC.neto_2 + rcdCC.neto_3 + rcdCC.neto_no_gravado
		rcdCom.plancta_id_gastos 	= rcdCC.plancta_id_gastos
		var p = null
		if(rcdCom.plancta_id_gastos == 0 || rcdCom.plancta_id_gastos == null)
		{
			p = fs_cc.ctacte_pr_to_proveedor.plancta_id_gastos
			if (p == 0 || p == null)
			{
				rcdCom.plancta_id_gastos = 424
			}
			else
			{
				rcdCom.plancta_id_gastos = p
			}
		}
		if (rcdCC.cod_mov == 2)
		{
			rcdCom.importe_cred = rcdCom.importe
			rcdCom.importe = 0
		}
		else
		{
			rcdCom.importe_cred = 0
		}
		databaseManager.saveData(rcdCom)
	}
	
	var query = 'SELECT plancta_id_gastos as plancta,SUM(importe) as total,SUM(importe_cred) as total_cred FROM compras_contable '+
			'group by plancta_id_gastos order by plancta_id_gastos;'
	var ds = databaseManager.getDataSetByQuery('infocom',query,null,-1)
	
	var frm = solutionModel.getForm('info_compras_contable_2')
	var tipos = new Array (JSColumn.INTEGER,JSColumn.NUMBER,JSColumn.NUMBER) 
	frm.dataSource = ds.createDataSource('totales',tipos)
	
	forms[frm.name].controller.recreateUI()
	globals.mostrarForm(frm.name)
}
